package com.home.action;

import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.home.po.Light;
import com.home.service.LightService;
import com.home.util.Tool;

@WebServlet(asyncSupported = true, urlPatterns = { "/LightAction" })
public class LightAction extends BaseAction {
	private static Logger logger = Logger.getLogger(BaseAction.class); 
	LightService lightService = new LightService();
	
	public void work(HttpServletRequest request) {
		System.out.println("this is a son work method");
	}
	
	public void query(HttpServletRequest request) {
		logger.info("execute LightAction query()");
		String start = request.getParameter("start");
		String pageSize = request.getParameter("pageSize");
		Light light = get(request);
		List<Light> lightList = lightService.query(light, Integer.valueOf(start), Integer.valueOf(pageSize));
	}
	
	//http://localhost/dataCenter/LightAction?oper=add&name=xx1&no=1000&state=2&time=2016-11-11 11:11:11&auto=1
	public void add(HttpServletRequest request) {
		logger.info("execute LightAction add()");
		Light light = get(request);
		lightService.addLight(light);
	}
	
	public void remove(HttpServletRequest request) {
		logger.info("execute LightAction remove()");
		String id = request.getParameter("id");
		lightService.remove(Integer.valueOf(id));
		
	}
	
	public void update(HttpServletRequest request) {
		logger.info("execute LightAction update()");
		Light light = get(request);
		lightService.update(light);
	}
	
	public Light get(HttpServletRequest request) {
		String name = request.getParameter("name");
		String no = request.getParameter("no");
		String state = request.getParameter("state");
		String time = request.getParameter("time");
		String auto = request.getParameter("auto");
		Light light = new Light();
		light.setName(name);
		light.setNo(Integer.valueOf(no));;
		light.setState(Integer.valueOf(state));
		light.setTime(Tool.stringToDate(time));
		light.setAuto(Integer.valueOf(auto));
		return light;
	}
	
	
	
	
	
}

